using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace CodeGenerator.Classes
{
    public class TableSQL
    {
        private string mTableName = "";
        private string connStr = "";

        private string columnName;
        private string dataType;
        private string maximumLength;
        //Column_Name,Data_Type,Character_Maximum_Length

        public string ColumnName
        {
            set { columnName = value; }
            get { return columnName; }
        }

        public string DataType
        {
            set { dataType = value; }
            get { return dataType; }
        }

        public string MaximumLength
        {
            set { maximumLength = value; }
            get { return maximumLength; }
        }

        public TableSQL()//string connectionString, string TableName)
        {
            //mTableName = TableName;
            //connStr = connectionString;
        }

        public TableSQL GetDataByColumn(string mTableName, string connStr,string mColumnName)
        {
            TableSQL result = new TableSQL();
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("SELECT Column_Name,Data_Type,Character_Maximum_Length From information_Schema.columns Where Table_Name='" + mTableName + "' AND Column_Name='"+mColumnName+"'", conn);
            SqlDataReader rdr;
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    result.columnName = rdr.GetString(0);
                    result.dataType = rdr.GetString(1);
                    
                    result.maximumLength = rdr[2]==DBNull.Value? "" : rdr.GetInt32(2).ToString();
                }
            }
            catch(Exception ex)
            {

            }

            return result;
        }
    }
}
